<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="net.contrend.system.form.MenuForm"%>
<%@page import="net.contrend.util.StringUtils"%>
<%
    pageContext.setAttribute("PAGETITLE", "菜单管理");

    List<MenuForm> menus = (List<MenuForm>) request.getAttribute("menus");
%>
<%!
    private String buildTree(List<MenuForm> menus, String div, String menuAction) {
        StringBuilder sb = new StringBuilder();
        this.appendln(sb, "var tree = new JsTree(\"" + div + "\");");
        this.appendln(sb, "tree.iconPath = ctx + \"/img/jstree/\";");
        this.appendln(sb, "var root = new JsNode(\"CWMS\");");
        String rootText = "<a href=\\\"javascript:" + menuAction + "('edit', {menuId: ''})\\\">CWMS</a>";
        this.appendln(sb, "root.text = \"" + rootText + "\";");
        this.appendln(sb, "tree.setRoot(root);");
        for (MenuForm menu : menus) {
            String text = "<a href=\\\"javascript:" + menuAction + "('edit', {menuId: '" + menu.getMenuId() + "'})\\\">" + menu.getMenuName() + "</a>";
            
            String vname = "menu" + menu.getMenuId().replaceAll("-", "");
            String pname = "menu" + menu.getParentId().replaceAll("-", "");
            this.appendln(sb, "var " + vname + " = new JsNode(\"" + vname + "\");");
            this.appendln(sb, vname + ".text = \"" + text + "\";");
            if (StringUtils.isNotBlank(menu.getParentId())) {
                this.appendln(sb, pname + ".add(" + vname + ");");
            } else {
                this.appendln(sb, "root.add(" + vname + ");");
            }
        }
        return sb.toString();
    }

    private void appendln(StringBuilder sb, String s) {
        sb.append(s).append("\n");
    }
%>
<%@ include file="/pages/include/header.jsp" %>

<style>
<!--
/* your css code */
-->
</style>

<script type="text/javascript">
<!--
function cmd(command, params) {
    switch (command) {
	    case "edit":
	        var url = ctx;
		    if (!params.menuId) {
			    url += "/pages/system/menuRoot.jsp";
            } else {
                url += "/action/menu.do?method=edit&menuId=" + params.menuId;
            }
	        $E("main").src = url;
	        break;
    }
}

$(document).ready(function () {
<%=this.buildTree(menus, "index_left", "cmd")%>
    $("#action_result").animate({left: -100}, 0);
});
//-->
</script>

<body>

    <div class="page_header">${PAGETITLE}</div>
    
    <div class="page_buttons">
        <div id="action_result" class="action_result">${requestScope.ACTION_RESULT}</div>
        <input type="button" onclick="$('#form').submit()"
            value="${empty form.map.form.menuId ? '保存' : '更新'}" />
    </div>

    <div class="index_left" id="index_left">
        
    </div>
    
    <div class="index_main">
        <iframe id="main" scrolling="auto" frameborder="0" width="100%" height="100%"
            src="${ctx}/pages/system/menuRoot.jsp"></iframe>
    </div>

</body>

<script type="text/javascript">
<!--
// your js code
//-->
</script>

<%@ include file="/pages/include/footer.jsp" %>
